package tool;

import io.PaukerImporter;

import javax.swing.JPanel;

import model.LessonGuide;
import view.CheckerDialog;
import view.CheckerDialogController;
import view.Controller;
import view.CreateCardDialog;
import view.CreateCardDialogController;
import view.CreateNewLessonDialog;
import view.CreateNewLessonDialogController;
import view.ImportDialog;
import view.ImportDialogController;
import view.MainFrame;
import view.MainFrameController;
import view.StatisticsPanel;
import view.StatisticsPanelController;
import view.UserCheckerController;
import view.UserCheckerView;
import eventHandling.EventBus;

/**
 * This class handles all dependences between all classes.
 * 
 * @author sloth42
 * 
 */
public class Factory {

	private EventBus eventBus;
	private MainFrame mainFrame;
	private MainFrameController mainFrameController;
	private LessonGuide lessonGuide;
	private PaukerImporter paukerImporter;
	private Controller createCardDialogController;
	private CreateCardDialog createCardDialog;
	private Controller createNewLessonDialogController;
	private CreateNewLessonDialog createNewLessonDialog;
	private ImportDialog importDialog;
	private Controller importDialogController;
	private StatisticsPanel statisticsPanel;
	private Controller statisticsPanelController;
	private CheckerDialog checkerDialog;
	private Controller checkerDialogController;
	private Controller userCheckerController;
	private UserCheckerView userCheckerView;

	public PaukerImporter getPaukerImporter() {
		if (paukerImporter == null) {
			paukerImporter = new PaukerImporter();
		}
		return paukerImporter;
	}

	public EventBus getEventBus() {
		if (eventBus == null) {
			eventBus = new EventBus();
		}
		return eventBus;
	}

	public LessonGuide getLessonGuide() {
		if (lessonGuide == null) {
			lessonGuide = new LessonGuide(getEventBus());
		}
		return lessonGuide;
	}

	public MainFrame getMainFrame() {
		if (mainFrame == null) {
			mainFrame = new MainFrame(getEventBus(), getStatisticsPanel());
		}
		return mainFrame;
	}

	public MainFrameController getMainFrameController() {
		if (mainFrameController == null) {
			mainFrameController = new MainFrameController(getEventBus(),
					getMainFrame(), getLessonGuide());
		}
		//
		// Sub Controller
		//
		mainFrameController.addSubController(getCreateCardDialogController());
		mainFrameController
				.addSubController(getCreateNewLessonDialogController());
		mainFrameController.addSubController(getImportDialogController());
		mainFrameController.addSubController(getStatisticsPanelController());
		mainFrameController.addSubController(getCheckerDialogController());

		return mainFrameController;
	}

	public Controller getCreateCardDialogController() {
		if (createCardDialogController == null) {
			createCardDialogController = new CreateCardDialogController(
					getEventBus(), getCreateCardDialog(), getLessonGuide());
		}
		return createCardDialogController;
	}

	public CreateCardDialog getCreateCardDialog() {
		if (createCardDialog == null) {
			createCardDialog = new CreateCardDialog(getEventBus(),
					getMainFrame());
		}
		return createCardDialog;
	}

	public Controller getCreateNewLessonDialogController() {
		if (createNewLessonDialogController == null) {
			createNewLessonDialogController = new CreateNewLessonDialogController(
					getEventBus(), getCreateNewLessonDialog(), getLessonGuide());
		}
		return createNewLessonDialogController;
	}

	public CreateNewLessonDialog getCreateNewLessonDialog() {
		if (createNewLessonDialog == null) {
			createNewLessonDialog = new CreateNewLessonDialog(getEventBus(),
					getMainFrame());
		}
		return createNewLessonDialog;
	}

	public ImportDialog getImportDialog() {
		if (importDialog == null) {
			importDialog = new ImportDialog(getEventBus());
		}
		return importDialog;
	}

	public Controller getImportDialogController() {
		if (importDialogController == null) {
			importDialogController = new ImportDialogController(getEventBus(),
					getMainFrame(), getImportDialog(), getLessonGuide());
		}
		return importDialogController;
	}

	public StatisticsPanel getStatisticsPanel() {
		if (statisticsPanel == null) {
			statisticsPanel = new StatisticsPanel(getEventBus());
		}
		return statisticsPanel;
	}

	public Controller getStatisticsPanelController() {
		if (statisticsPanelController == null) {
			statisticsPanelController = new StatisticsPanelController(
					getEventBus(), getStatisticsPanel(), getLessonGuide());
		}
		return statisticsPanelController;
	}

	public CheckerDialog getCheckerDialog() {
		if (checkerDialog == null) {
			checkerDialog = new CheckerDialog(getEventBus(), getMainFrame());
		}
		return checkerDialog;
	}

	public Controller getCheckerDialogController() {
		if (checkerDialogController == null) {
			checkerDialogController = new CheckerDialogController(
					getEventBus(), getCheckerDialog());

			((CheckerDialogController) checkerDialogController)
					.addSubcontroller(getUserCheckerController());
		}
		return checkerDialogController;
	}

	public Controller getUserCheckerController() {
		if (userCheckerController == null) {

			userCheckerController = (Controller) new UserCheckerController(
					getEventBus(), getUserCheckerView(), getCheckerDialog()
							.getCheckerButtonsPanel());
		}
		return userCheckerController;
	}

	public UserCheckerView getUserCheckerView() {
		if (userCheckerView == null) {
			userCheckerView = new UserCheckerView(getEventBus());
		}
		return userCheckerView;
	}

}
